import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class FindOrder {

    public static void main(String[] args) {
        int[][] nums = {{1, 0}, {0, 1}};
        FindOrder findOrder = new FindOrder();
        System.out.println(Arrays.toString(findOrder.findOrder(2, nums)));
    }

    public int[] findOrder(int numCourses, int[][] prerequisites) {
        List<List<Integer>> edge = new ArrayList<>();
        LinkedList<Integer> queueu = new LinkedList<>();
        int[] inDeg = new int[numCourses];
        int[] ans = new int[numCourses];
        for (int i = 0; i < numCourses; i++) {
            edge.add(new ArrayList<>());
        }
        for (int[] info : prerequisites) {
            edge.get(info[1]).add(info[0]);
            inDeg[info[0]]++;
        }
        for (int i = 0; i < numCourses; i++) {
            if (inDeg[i] == 0) queueu.add(i);
        }
        int i = 0;
        while (!queueu.isEmpty()) {
            Integer poll = queueu.poll();
            for (Integer index : edge.get(poll)) {
                inDeg[index]--;
                if (inDeg[index] == 0) queueu.add(index);
            }
            ans[i++] = poll;
        }
        return i == numCourses ? ans : new int[0];
    }
}
